package servlet;

import net.sf.json.JSONObject;
import util.RequestUtil;
import util.ResponseUtil;
import util.TokenUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author fzh
 * @since 2017/11/9
 */
@WebServlet(name = "StateCheckServlet", urlPatterns = "/validator")
public class StateCheckServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject json = RequestUtil.getJsonFromRequest(request);
        if (json != null) {
            String token = (String) json.get("token");
            if (TokenUtil.availableJWT(token)) {
                ResponseUtil.writeSuccess(response);
            } else {
                ResponseUtil.writeNotFound(response);
            }
        } else {
            ResponseUtil.writeBadRequest(response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResponseUtil.writeBadRequest(response);
    }
}
